<script setup>
import { Head, Link, useForm } from '@inertiajs/vue3'
import Layout from '@/Layouts/Layout.vue'
import TextInput from '@/Components/TextInputPing.vue'
import SelectInput from '@/Components/SelectInput.vue'
import LoadingButton from '@/Components/LoadingButton.vue'
import TrashedMessage from '@/Components/TrashedMessage.vue'

const props = defineProps({
    contact: Object,
    organizations: Array,
})

const form = useForm({
    first_name: props.contact.first_name,
    last_name: props.contact.last_name,
    organization_id: props.contact.organization_id,
    email: props.contact.email,
    phone: props.contact.phone,
    address: props.contact.address,
    city: props.contact.city,
    region: props.contact.region,
    country: props.contact.country,
    postal_code: props.contact.postal_code,
})

function update() {
    form.put(`/contacts/${props.contact.id}`)
}

function destroy() {
    if (confirm('Are you sure you want to delete this contact?')) {
        form.delete(`/contacts/${props.contact.id}`)
    }
}

function restore() {
    if (confirm('Are you sure you want to restore this contact?')) {
        form.put(`/contacts/${props.contact.id}/restore`)
    }
}
</script>

<template>
    <Layout>
        <div>
            <Head :title="`${form.first_name} ${form.last_name}`" />
            <div class="flex justify-start mb-8 max-w-3xl">
                <h1 class="text-3xl font-bold">
                    <Link class="text-indigo-400 hover:text-indigo-600" :href="route('contacts.index')">Contacts</Link>
                    <span class="text-indigo-400 font-medium">/</span>
                    {{ form.first_name }} {{ form.last_name }}
                </h1>
            </div>
            <TrashedMessage v-if="contact.deleted_at" class="mb-6" @restore="restore"> This contact has been deleted. </TrashedMessage>
            <div class="max-w-3xl bg-white rounded-md shadow overflow-hidden">
                <form @submit.prevent="update">
                    <div class="flex flex-wrap -mb-8 -mr-6 p-8">
                        <TextInput v-model="form.first_name" :error="form.errors.first_name" class="pb-8 pr-6 w-full lg:w-1/2" label="First name" />
                        <TextInput v-model="form.last_name" :error="form.errors.last_name" class="pb-8 pr-6 w-full lg:w-1/2" label="Last name" />
                        <SelectInput v-model="form.organization_id" :error="form.errors.organization_id" class="pb-8 pr-6 w-full lg:w-1/2" label="Organization">
                            <option :value="null" />
                            <option v-for="organization in organizations" :key="organization.id" :value="organization.id">{{ organization.name }}</option>
                        </SelectInput>
                        <TextInput v-model="form.email" :error="form.errors.email" class="pb-8 pr-6 w-full lg:w-1/2" label="Email" />
                        <TextInput v-model="form.phone" :error="form.errors.phone" class="pb-8 pr-6 w-full lg:w-1/2" label="Phone" />
                        <TextInput v-model="form.address" :error="form.errors.address" class="pb-8 pr-6 w-full lg:w-1/2" label="Address" />
                        <TextInput v-model="form.city" :error="form.errors.city" class="pb-8 pr-6 w-full lg:w-1/2" label="City" />
                        <TextInput v-model="form.region" :error="form.errors.region" class="pb-8 pr-6 w-full lg:w-1/2" label="Region" />
                        <SelectInput v-model="form.country" :error="form.errors.country" class="pb-8 pr-6 w-full lg:w-1/2" label="Country">
                            <option :value="null" />
                            <option value="CA">Canada</option>
                            <option value="US">United States</option>
                        </SelectInput>
                        <TextInput v-model="form.postal_code" :error="form.errors.postal_code" class="pb-8 pr-6 w-full lg:w-1/2" label="Postal code" />

                    </div>
                    <div class="flex items-center px-8 py-4 bg-gray-50 border-t border-gray-100">
                        <button v-if="!contact.deleted_at" class="text-red-600 hover:underline" tabindex="-1" type="button" @click="destroy">Delete Contact</button>
                        <LoadingButton :loading="form.processing" class="btn-indigo ml-auto" type="submit">Update Contact</LoadingButton>
                    </div>
                </form>
            </div>
        </div>
    </Layout>
    
</template>